//
//  StartViewController.m
//  FrogDrying
//
//  Created by Le Thanh Hai on 5/15/13.
//
//

#import "StartViewController.h"

@interface StartViewController ()

@end

@implementation StartViewController
@synthesize delegate;

-(IBAction)loginFacebook:(id)sender
{
    [[ShareFacebook shareInstance] authorize];
}

-(IBAction)startGame:(id)sender
{
    if (delegate != nil && [delegate respondsToSelector:@selector(playGame)]) {
        [delegate playGame];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIView animateWithDuration:0.4 animations:^{
        self.view.alpha = 1;
        if ([ShareFacebook canUseFacebook]) {            
            [[ShareFacebook shareInstance] getRankingList:^(id data) {
                rankingList.dataSource = [data copy];
                inviteFriend.hidden = YES;
                rankingList.hidden = NO;
            }];
        }
    }];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIView animateWithDuration:0.4 animations:^{
        self.view.alpha = 0;
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([[ShareFacebook shareInstance] statusLogin] ) {
        btnLogin.hidden = YES;
        text.hidden = YES;
        
        [[ShareFacebook shareInstance] getRankingList:^(id data) {
            rankingList.dataSource = [data copy];
            inviteFriend.hidden = YES;
            rankingList.hidden = NO;
        }];
    } else {
        btnRefresh.highlighted = YES;
        btnRefresh.userInteractionEnabled = NO;
        
        btnInvite.highlighted = YES;
        btnInvite.userInteractionEnabled = NO;
    }
    
    NSArray *permissions =  [NSArray arrayWithObjects:
                             @"publish_actions", nil];
    
    [[ShareFacebook shareInstance] setPermissions:permissions];
    [[ShareFacebook shareInstance] setDelegate:self];
    rankingList.backgroundColor = [UIColor clearColor];
}

-(IBAction)refreshRanking:(id)sender
{
    [[ShareFacebook shareInstance] getRankingList:^(id data) {
        rankingList.dataSource = [data copy];
        inviteFriend.hidden = YES;
        rankingList.hidden = NO;
    }];
}

-(IBAction)inviteFriend:(id)sender
{
    
    [[ShareFacebook shareInstance] getAllFriend:^(id data) {
        inviteFriend.dataSource = [data copy];
        rankingList.hidden = YES;
        inviteFriend.hidden = NO;
    }];
}

#pragma mark -- sharefacebook 
-(void)fbDidLogin
{
    btnLogin.hidden = YES;
    text.hidden = YES;
    
    btnRefresh.highlighted = NO;
    btnRefresh.userInteractionEnabled = YES;
    
    btnInvite.highlighted = NO;
    btnInvite.userInteractionEnabled = YES;
    
    [[ShareFacebook shareInstance] getRankingList:^(id data) {
        rankingList.dataSource = [data copy];
        inviteFriend.hidden = YES;
        rankingList.hidden = NO;
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// Override to allow orientations other than the default portrait orientation.
#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    BOOL shouldAutorotate = NO;
    
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
        || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        
        shouldAutorotate = YES;
    }
    
    return shouldAutorotate;
}
#endif

#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate {
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}
#endif
@end
